import * as Blockly from 'blockly/core';

const BLOCK_TOOL_TYPE_HUE = 120;

const blocks = {};

const typeBlockCheckPos = (block) => {
    const parent = block.getSurroundParent();
    if (parent && [
        'block_tool_value_input',
        'block_tool_statement_input',
        'block_tool_dummy_input'
    ].includes(parent.type)) {
        return true;
    }
    return false;
}

const typeBlockSetWarningText = (block) => {
    if (typeBlockCheckPos(block)) {
        block.setWarningText(null);
    } else {
        block.setWarningText("此块需放到输入块下面");
    }
}

const typeBlockDropdownItemCheckPos = (block) => {
    const parent = block.getSurroundParent();
    if (parent && ['block_tool_type_dropdown', 'block_tool_type_grid_dropdown'].includes(parent.type)) {
        return true;
    }
    return false;
}

const typeBlockDropdownItemSetWarningText = (block) => {
    if (typeBlockDropdownItemCheckPos(block)) {
        block.setWarningText(null);
    } else {
        block.setWarningText("此块需放到下拉框块下面");
    }
}

blocks.block_tool_type_text = {
    init: function () {
        this.appendDummyInput()
            .appendField("文本:")
            .appendField(new Blockly.FieldTextInput(""), "VALUE");
        this.setPreviousStatement(true, "BLOCK_TOOL_INPUT_BLOCK");
        this.setNextStatement(true, "BLOCK_TOOL_INPUT_BLOCK");
        this.setColour(BLOCK_TOOL_TYPE_HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    onchange: function () {
        typeBlockSetWarningText(this);
    }
};

blocks.block_tool_type_text_input = {
    init: function () {
        this.appendDummyInput()
            .appendField("单行文本输入:")
            .appendField(new Blockly.FieldTextInput("data"), "VALUE")
            .appendField(",")
            .appendField(new Blockly.FieldTextInput("NAME"), "NAME");
        this.setPreviousStatement(true, "BLOCK_TOOL_INPUT_BLOCK");
        this.setNextStatement(true, "BLOCK_TOOL_INPUT_BLOCK");
        this.setColour(BLOCK_TOOL_TYPE_HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    onchange: function () {
        typeBlockSetWarningText(this);
    }
};

blocks.block_tool_type_multiline_input = {
    init: function () {
        this.appendDummyInput()
            .appendField("多行文本输入:")
            .appendField(new Blockly.FieldMultilineInput("data"), "VALUE")
            .appendField(",")
            .appendField(new Blockly.FieldTextInput("NAME"), "NAME");
        this.setPreviousStatement(true, "BLOCK_TOOL_INPUT_BLOCK");
        this.setNextStatement(true, "BLOCK_TOOL_INPUT_BLOCK");
        this.setColour(BLOCK_TOOL_TYPE_HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    onchange: function () {
        typeBlockSetWarningText(this);
    }
};

blocks.block_tool_type_label_input = {
    init: function () {
        this.appendDummyInput()
            .appendField("标签输入:")
            .appendField(new Blockly.FieldTextInput("data"), "VALUE")
            .appendField(",")
            .appendField(new Blockly.FieldTextInput("NAME"), "NAME");
        this.setPreviousStatement(true, "BLOCK_TOOL_INPUT_BLOCK");
        this.setNextStatement(true, "BLOCK_TOOL_INPUT_BLOCK");
        this.setColour(BLOCK_TOOL_TYPE_HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    onchange: function () {
        typeBlockSetWarningText(this);
    }
};

blocks.block_tool_type_number_slider = {
    init: function () {
        this.appendDummyInput()
            .appendField(new Blockly.FieldDropdown([
                ["数字输入", "FieldNumber"],
                ["滑块", "FieldSlider"]
            ]), "TYPE")
            .appendField(":")
            .appendField(new Blockly.FieldTextInput("0"), "VALUE")
            .appendField(",")
            .appendField(new Blockly.FieldTextInput("NAME"), "NAME");
        this.appendDummyInput()
            .appendField("最小:")
            .appendField(new Blockly.FieldTextInput("-1"), "MIN")
            .appendField("最大:")
            .appendField(new Blockly.FieldTextInput("1"), "MAX")
            .appendField("精度:")
            .appendField(new Blockly.FieldTextInput("1"), "P");
        this.setPreviousStatement(true, "BLOCK_TOOL_INPUT_BLOCK");
        this.setNextStatement(true, "BLOCK_TOOL_INPUT_BLOCK");
        this.setColour(BLOCK_TOOL_TYPE_HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    onchange: function () {
        typeBlockSetWarningText(this);
    }
};

blocks.block_tool_type_angle = {
    init: function () {
        this.appendDummyInput()
            .appendField("角度输入:")
            .appendField(new Blockly.FieldAngle(90), "VALUE")
            .appendField(",")
            .appendField(new Blockly.FieldTextInput("NAME"), "NAME");
        this.setPreviousStatement(true, "BLOCK_TOOL_INPUT_BLOCK");
        this.setNextStatement(true, "BLOCK_TOOL_INPUT_BLOCK");
        this.setColour(BLOCK_TOOL_TYPE_HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    onchange: function () {
        typeBlockSetWarningText(this);
    }
};

blocks.block_tool_type_date = {
    init: function () {
        this.appendDummyInput()
            .appendField("日期输入:")
            .appendField(new Blockly.FieldDate(), "VALUE")
            .appendField(",")
            .appendField(new Blockly.FieldTextInput("NAME"), "NAME");
        this.setPreviousStatement(true, "BLOCK_TOOL_INPUT_BLOCK");
        this.setNextStatement(true, "BLOCK_TOOL_INPUT_BLOCK");
        this.setColour(BLOCK_TOOL_TYPE_HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    onchange: function () {
        typeBlockSetWarningText(this);
    }
};

blocks.block_tool_type_dropdown = {
    init: function () {
        this.appendDummyInput()
            .appendField("下拉框(单列),")
            .appendField(new Blockly.FieldTextInput("NAME"), "NAME");
        this.appendStatementInput("VALUE")
            .setCheck("BLOCK_TOOL_TYPE_DROPDOWN")
            .appendField("设计为");
        this.setPreviousStatement(true, "BLOCK_TOOL_INPUT_BLOCK");
        this.setNextStatement(true, "BLOCK_TOOL_INPUT_BLOCK");
        this.setColour(BLOCK_TOOL_TYPE_HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    onchange: function () {
        typeBlockSetWarningText(this);
    }
};

blocks.block_tool_type_grid_dropdown = {
    init: function () {
        this.appendDummyInput()
            .appendField("下拉框(多列),")
            .appendField(new Blockly.FieldTextInput("NAME"), "NAME");
        this.appendDummyInput()
            .appendField("列数:")
            .appendField(new Blockly.FieldNumber(3, 2, 16, 1), "COL");
        this.appendStatementInput("VALUE")
            .setCheck("BLOCK_TOOL_TYPE_DROPDOWN")
            .appendField("设计为");
        this.setPreviousStatement(true, "BLOCK_TOOL_INPUT_BLOCK");
        this.setNextStatement(true, "BLOCK_TOOL_INPUT_BLOCK");
        this.setColour(BLOCK_TOOL_TYPE_HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    onchange: function () {
        typeBlockSetWarningText(this);
    }
};

blocks.block_tool_type_dropdown_text = {
    init: function () {
        this.appendDummyInput()
            .appendField("显示文本:")
            .appendField(new Blockly.FieldTextInput("data1"), "VALUE")
            .appendField(".")
            .appendField(new Blockly.FieldTextInput("NAME"), "NAME");
        this.setPreviousStatement(true, "BLOCK_TOOL_TYPE_DROPDOWN");
        this.setNextStatement(true, "BLOCK_TOOL_TYPE_DROPDOWN");
        this.setColour("#cc66cc");
        this.setTooltip("");
        this.setHelpUrl("");
    },
    onchange: function () {
        typeBlockDropdownItemSetWarningText(this);
    }
};

blocks.block_tool_type_dropdown_image = {
    init: function () {
        this.appendDummyInput()
            .appendField("图片")
            .appendField(new Blockly.FieldTextInput("../common/media/mixly.png"), "VALUE");
        this.appendDummyInput()
            .appendField("宽度:")
            .appendField(new Blockly.FieldTextInput("20"), "WIDTH")
            .appendField("高度:")
            .appendField(new Blockly.FieldTextInput("20"), "HEIGHT")
            .appendField("替代文本:")
            .appendField(new Blockly.FieldTextInput("*"), "ALT")
            .appendField(".")
            .appendField(new Blockly.FieldTextInput("NAME"), "NAME");
        this.setPreviousStatement(true, "BLOCK_TOOL_TYPE_DROPDOWN");
        this.setNextStatement(true, "BLOCK_TOOL_TYPE_DROPDOWN");
        this.setColour("#cc66cc");
        this.setTooltip("");
        this.setHelpUrl("");
    },
    onchange: function () {
        typeBlockDropdownItemSetWarningText(this);
    }
};

blocks.block_tool_type_bitmap = {
    init: function () {
        this.appendDummyInput()
            .appendField("位图,")
            .appendField(new Blockly.FieldTextInput("NAME"), "NAME");
        this.appendDummyInput()
            .appendField("宽度:")
            .appendField(new Blockly.FieldNumber(8, 1, 24, 1), "WIDTH")
            .appendField("高度:")
            .appendField(new Blockly.FieldNumber(8, 1, 24, 1), "HEIGHT")
            .appendField("填充:")
            .appendField(new Blockly.FieldColourHsvSliders("#000"), "FILL_COLOR")
            .appendField("空白:")
            .appendField(new Blockly.FieldColourHsvSliders("#5ba5a5"), "EMPTY_COLOR")
            .appendField("网格:")
            .appendField(new Blockly.FieldColourHsvSliders("#e5e7f1"), "BG_COLOR");
        this.setPreviousStatement(true, "BLOCK_TOOL_INPUT_BLOCK");
        this.setNextStatement(true, "BLOCK_TOOL_INPUT_BLOCK");
        this.setColour(BLOCK_TOOL_TYPE_HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    onchange: function () {
        typeBlockSetWarningText(this);
    }
};

blocks.block_tool_type_checkbox = {
    init: function () {
        this.appendDummyInput()
            .appendField("检查框:")
            .appendField(new Blockly.FieldCheckbox("TRUE"), "VALUE")
            .appendField(",")
            .appendField(new Blockly.FieldTextInput("NAME"), "NAME");
        this.setPreviousStatement(true, "BLOCK_TOOL_INPUT_BLOCK");
        this.setNextStatement(true, "BLOCK_TOOL_INPUT_BLOCK");
        this.setColour(BLOCK_TOOL_TYPE_HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    onchange: function () {
        typeBlockSetWarningText(this);
    }
};

blocks.block_tool_type_color = {
    init: function () {
        this.appendDummyInput()
            .appendField("颜色(固定):")
            .appendField(new Blockly.FieldColour("#ff0000"), "VALUE")
            .appendField(",")
            .appendField(new Blockly.FieldTextInput("NAME"), "NAME");
        this.setPreviousStatement(true, "BLOCK_TOOL_INPUT_BLOCK");
        this.setNextStatement(true, "BLOCK_TOOL_INPUT_BLOCK");
        this.setColour(BLOCK_TOOL_TYPE_HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    onchange: function () {
        typeBlockSetWarningText(this);
    }
};

blocks.block_tool_type_color_hsv_sliders = {
    init: function () {
        this.appendDummyInput()
            .appendField("颜色(动态):")
            .appendField(new Blockly.FieldColourHsvSliders("#ff0000"), "VALUE")
            .appendField(",")
            .appendField(new Blockly.FieldTextInput("NAME"), "NAME");
        this.setPreviousStatement(true, "BLOCK_TOOL_INPUT_BLOCK");
        this.setNextStatement(true, "BLOCK_TOOL_INPUT_BLOCK");
        this.setColour(BLOCK_TOOL_TYPE_HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    onchange: function () {
        typeBlockSetWarningText(this);
    }
};

blocks.block_tool_type_image = {
    init: function () {
        this.appendDummyInput()
            .appendField("图片")
            .appendField(new Blockly.FieldTextInput("../common/media/mixly.png"), "VALUE");
        this.appendDummyInput()
            .appendField("宽度:")
            .appendField(new Blockly.FieldTextInput("20"), "WIDTH")
            .appendField("高度:")
            .appendField(new Blockly.FieldTextInput("20"), "HEIGHT")
            .appendField("替代文本:")
            .appendField(new Blockly.FieldTextInput("*"), "ALT")
        this.setPreviousStatement(true, "BLOCK_TOOL_INPUT_BLOCK");
        this.setNextStatement(true, "BLOCK_TOOL_INPUT_BLOCK");
        this.setColour(BLOCK_TOOL_TYPE_HUE);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    onchange: function () {
        typeBlockSetWarningText(this);
    }
};

export default blocks;